package org.example.code.test2024.eight_mouth;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// 必须定义 `ShowMeBug` 入口类和 `public static void main(String[] args)` 入口方法
public class ShowMeBug{
    private double balance; // 账户余额
    private Lock accountLock = new ReentrantLock();

    /**
     * 存款
     *
     * @param money 存入金额
     */
    public void deposit(double money) {
        accountLock.lock();            //加锁

        double newBalance = balance + money;

        try {
            Thread.sleep(10);        //模拟此业务需要处理时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        balance = newBalance;

        accountLock.unlock();        //解锁
    }

    /**
     * 获得账户余额
     */
    public double getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        ShowMeBug account = new ShowMeBug();

        System.out.println("账户余额: " + account.getBalance());
    }
}